<template>
	<view>
		<haverster-slidingMenu :list="list" @changes="changes"></haverster-slidingMenu>
		<view class="example-body">
			<uni-search-bar placeholder="请输入订单号进行查询" @cancel="cancel" />
		</view>
		<uni-card
				v-for="(item, i) in orders"
				:key="i"
		    :title="item.order_no"
		    mode="title"
		    :is-shadow="true"
		    :extra="item.order_status"
		    :note="item.id"
				:stock="item.pay_no"
				:size="item.id"
		>
		<view class="uni-padding-wrap">
			创建时间: {{ item.order_createtime }}
			<text class="ckxq" @click="goodInfo(item.order_no)">查看详情</text>
			<view class="dibu">
				<text class="ckxq" v-if="item.order_status == '交易成功'" @click="queren(item.order_no)">确认收货</text>
				<text style="margin-right: 10upx;">订单状态: {{ item.order_status }}</text>
			</view>
		</view>
		
		</uni-card>
		<uni-load-more :status="more" @onMore="onMore" :content-text="contentText" color="#007aff" />
	</view>
</template>

<script>
	import haversterSlidingMenu from "@/components/haverster-slidingMenu/haversterSlidingMenu.vue"
	import uniCard from "@/components/uni-card/uniOrder-card"
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue"
	import uniSearchBar from '@/components/uni-search-bar/uni-search-bar.vue'
	
	export default {
		components: { uniCard, uniLoadMore, haversterSlidingMenu, uniSearchBar },
		data() {
			return {
				navIndex: 0,
				list: ['进货订单', '销售订单'],
				orders: [],
				// more（loading前）、loading（loading中）、noMore（没有更多了）
				more: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多数据了！'
				},
				// 分页数据
				page: {
					pageIndex: 1,
					pageSize: 10
				}
			}
		},
		onLoad() {
			let params = {
				page: this.page.pageIndex,
				limit: this.page.pageSize,
			};
			params.orderType = 0
			this.init(params);
		},
		methods: {
			// 获取列表
			init(params) {
				this.$api.selectOrder(params).then((res) => {
					if (params.orderNo != undefined) {
						this.orders = res.rows
						this.more = 'noMore'
						return
					}
					if (!res.rows.length) {
						if (this.page.pageIndex == 1) {
							this.orders = []
						}
						this.more = 'noMore'
						return
					}
					res.rows.forEach(item => {
						if (item.order_status == 0) {
							item.order_status = '未支付'
						} else if (item.order_status == 1) {
							item.order_status = '交易成功'
						} else if (item.order_status == -1) {
							item.order_status = '交易失败'
						} else if (item.order_status == 2) {
							item.order_status = '取消订单'
						} else if (item.order_status == 3) {
							item.order_status = '已收货'
						}
						if (item.pay_type == 0) {
							item.pay_type = '线下支付'
						} else if (item.pay_type == 1) {
							item.pay_type = '线上支付'
						}
						if (item.status == 0) {
							item.status = '下架'
						} else if (item.status == 1) {
							item.status = '上架'
						} else  {
							item.status = '删除'
						}
					})
					if (res.total <= 10) {
						this.more = 'noMore'
					} else {
						this.more = 'more'
					}
					
					if (this.page.pageIndex == 1) {
						this.orders =res.rows
					} else {
						this.orders = [ ...this.orders, ...res.rows]
					}
				}).catch((err) => {
					console.log('request fail', err);
				})
			},
			
			// 加载更多
			onMore() {
				if (this.more === 'noMore') {
					return
				}
				this.page.pageIndex = this.page.pageIndex + 1
				let params = {
					page: this.page.pageIndex,
					limit: this.page.pageSize,
				};
				console.log(this.page.pageIndex)
				this.init(params);
			},
			
			// 点击搜索
			cancel(res) {
				let params = {
					page: this.page.pageIndex,
					limit: this.page.pageSize,
				};
				params.orderNo = res.value
				this.init(params);
			},
			
			// 点击导航买入卖出
			changes(index) {
				console.log(index)
				if (index === 0) {
					let params = {
						page: this.page.pageIndex,
						limit: this.page.pageSize,
					};
					params.userId = uni.getStorageSync('user').roleid
					params.orderType = 0
					this.init(params);
				} else if (index === 1) {
					let params = {
						page: this.page.pageIndex,
						limit: this.page.pageSize,
					};
					params.sellerId = uni.getStorageSync('user').roleid
					params.orderType = 1
					this.init(params);
				}
			},
			
			// 查看详情
			goodInfo(order_no) {
				uni.navigateTo({
				   url: './orderInfo?order_no=' + order_no
				});
			},
			
			// 确认收货
			queren(order_no) {
				let params = {
					orderNo: order_no
				}
				uni.showModal({
				    title: '提示',
				    content: '你确定收货么?',
				    success: (res) => {
							if (res.confirm) {
								this.$api.confirmOrder(params).then((res) => {
									if (res.success) {
										uni.showToast({
											icon: 'success',
											title: "成功,已移动到商品库",
											duration: 2000
										});
										let params = {
											page: this.page.pageIndex,
											limit: this.page.pageSize,
										};
										params.orderType = 0
										this.init(params);
									} else {
										uni.showToast({
											icon: 'none',
											title: res.error,
											duration: 2000
										});
									}
								}).catch((err) => {
									console.log('request fail', err);
								})
							} else if (res.cancel) {
									console.log('用户点击取消');
							}
				    }
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.ckxq {
		background: #007AFF;
		display: inline-block;
		width: 140upx;
		color: #fff;
		text-align: center;
		float: right;
		border-radius: 100px;
	}
	.dibu {
		margin-top: 10upx;
		float: left;
	}
</style>
